package com.songlanyun.modules.account.controller.admin;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.songlanyun.common.annotation.AuthIgnore;
import com.songlanyun.common.utils.DateUtils;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.account.entity.AccountOperationLogEntity;
import com.songlanyun.modules.account.model.vo.AccountOperationLogVO;
import com.songlanyun.modules.account.service.AccountOperationLogService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.activation.MimetypesFileTypeMap;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 会员操作日志的控制器
 *
 * @author 王熙伟(wang-xiwei@foxmail.com)
 * @since 2024-04-24
 */
@RestController
@RequestMapping("admin/account/operationlog")
public class AccountOperationLogAdminController extends AbstractController {

    @Resource
    private AccountOperationLogService accountOperationLogService;

    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 会员操作日志的列表查询
     */
    @GetMapping("/list")
    // @RequiresPermissions("account:yxtaccount:list")
    public R list(@NotNull @RequestParam Map<String, Object> params) {

        // 处理查询参数：startTime
        String startTime = String.valueOf(params.get("startTime"));
        if(StringUtils.isNotBlank(startTime) && !StringUtils.equalsIgnoreCase("null",startTime)){
            Date startDate = null;
            try {
                startDate = sdf.parse(startTime);
            } catch (ParseException e) {
                //
            }
            params.put("startTime", startDate);
        }

        // 处理查询参数：endTime
        String endTime = String.valueOf(params.get("endTime"));
        if(StringUtils.isNotBlank(endTime) && !StringUtils.equalsIgnoreCase("null",endTime)){
            Date endDate = null;
            try {
                endDate = sdf.parse(endTime);
            } catch (ParseException e) {
                //
            }
            params.put("endTime", endDate);
        }

        PageUtils page = accountOperationLogService.queryPage(params);
        return R.ok().put("page", page);
    }

    @GetMapping("/export")
    @AuthIgnore
    //@RequiresPermissions("usergains:distributionuserordergains:list")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        Long adminId = this.getAdminByToken(response);//校验token

        // 处理查询参数：startTime
        String startTime = String.valueOf(params.get("startTime"));
        if(StringUtils.isNotBlank(startTime) && !StringUtils.equalsIgnoreCase("null",startTime)){
            Date startDate = null;
            try {
                startDate = sdf.parse(startTime);
            } catch (ParseException e) {
                //
            }
            params.put("startTime", startDate);
        }

        // 处理查询参数：endTime
        String endTime = String.valueOf(params.get("endTime"));
        if(StringUtils.isNotBlank(endTime) && !StringUtils.equalsIgnoreCase("null",endTime)){
            Date endDate = null;
            try {
                endDate = sdf.parse(endTime);
            } catch (ParseException e) {
                //
            }
            params.put("endTime", endDate);
        }
        List<AccountOperationLogVO> logList = accountOperationLogService.exportList(params);

        String fileName = "会员操作日志列表" + DateUtils.format(new Date(), "yyyy-MM-dd-HHmmss") + "_" + adminId + "." + "xls";
        ExportParams exportParams = new ExportParams("会员操作日志列表导出(" + logList.size() + ")", "会员操作日志列表");

        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, AccountOperationLogVO.class, logList);
        response.setContentType(new MimetypesFileTypeMap().getContentType("application/octet-stream"));// 设置response内容的类型
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));// 设置头部信息
        sheets.write(response.getOutputStream());
        sheets.close();
    }

}
